package fpt.edu.vn.dao;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.classic.Session;
import org.hsqldb.lib.StringUtil;

import fpt.edu.vn.model.Activityhistorys;
import fpt.edu.vn.model.Districts;
import fpt.edu.vn.model.Favorites;
import fpt.edu.vn.model.Provinces;
import fpt.edu.vn.model.RefreshTopic;
import fpt.edu.vn.model.Statustopics;
import fpt.edu.vn.model.Topicimages;
import fpt.edu.vn.model.Topics;
import fpt.edu.vn.model.Topictypes;
import fpt.edu.vn.util.HibernateUtil;

/* 
 * TopicsDao.java
 *  
 * 0.1
 * 
 * 10/15/2012 
 *  
 * Copyright notice 
 *  
 * Modification Logs: 
 *   DATE        AUTHOR      DESCRIPTION 
 *  -------------------------------------------------------- 
 *  15-Oct-2012  ThuongND    Create new 
 */
public class TopicsDao {
	// get activity Log
	@SuppressWarnings("unchecked")
	public List<Object> adminShowActivityhistorys() {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<Object> lstActivity = null;
		try {
			String hql = "select a.activityHistoryId,a.ipAddress,a.userName ,a.topicId,a.activityTypeId,a.createDate,t.description, a.title, a.districtId "
					+ "from Activityhistorys a,Activitytypes t "
					+ "where a.activityTypeId = t.activityTypeId order by a.createDate desc";
			Query query = session.createQuery(hql);
			lstActivity = query.list();
		} catch (Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		session.getTransaction().commit();
		return lstActivity;
	}

	@SuppressWarnings("unchecked")
	public List<Object> listalltopicSpam(String search_field,
			String search_author, int city, int district, String fromnewsdate,
			String tonewsdate, int search_topic_type, String search_order_d,
			int search_topic_status) {

		// Hiện lên ở trang chủ
		// Nội dung các tin đăng
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<Object> topics = null;
		try {
			StringBuffer hsql = new StringBuffer();
			hsql.append("select t.title, t.address, t.viewNum,DATE_FORMAT( t.createDate, '%Y-%m-%d'),t.topicId, t.topicTypeId,to.topicTypeName , t.districtId, u.displayName, d.districtName, t.usersId, d.provinceId,p.provinceName,s.statusTopicName,t.statusTopicId,(select u.displayName from u where u.usersId = r.usersId),r.description from Topics t,Topictypes to, Districts d, Users u,Provinces p,Statustopics s,Reportspams r ");
			hsql.append("where p.provinceId = d.provinceId and t.districtId = d.districtId and t.usersId = u.usersId and to.topicTypeId = t.topicTypeId and t.statusTopicId = s.statusTopicId and r.topicId = t.topicId ");
			if (!StringUtil.isEmpty(search_field))
				hsql.append("AND LOWER(t.title) LIKE :title ");
			if (!StringUtil.isEmpty(search_author))
				hsql.append("AND LOWER(u.displayName) LIKE :displayName ");
			if (city != 0)
				hsql.append("AND LOWER(d.provinceId) = :provinceId ");
			if (district != 0)
				hsql.append("AND LOWER(t.districtId) = :districtId ");
			if (!StringUtil.isEmpty(fromnewsdate))
				hsql.append("AND DATE_FORMAT( t.createDate, '%Y-%m-%d') >= :fromnewsdate ");
			if (!StringUtil.isEmpty(tonewsdate))
				hsql.append("AND DATE_FORMAT( t.createDate, '%Y-%m-%d') <= :tonewsdate ");
			if (search_topic_type != 0)
				hsql.append("AND LOWER(t.topicTypeId) = :topicTypeId ");
			if (search_topic_status != 0)
				hsql.append("AND LOWER(t.statusTopicId) = :statusTopicId ");

			String order = "";
			if ("desc".equals(search_order_d))
				order = "order by t.renewDate desc";
			if ("asc".equals(search_order_d))
				order = "order by t.renewDate asc";
			hsql.append(order);

			Query query = session.createQuery(hsql.toString());
			if (!StringUtil.isEmpty(search_field))
				query.setParameter("title", "%" + search_field + "%");
			if (!StringUtil.isEmpty(search_author))
				query.setParameter("displayName", "%" + search_author + "%");
			if (city != 0)
				query.setParameter("provinceId", city);
			if (district != 0)
				query.setParameter("districtId", district);
			if (!StringUtil.isEmpty(fromnewsdate))
				query.setParameter("fromnewsdate", fromnewsdate);
			if (!StringUtil.isEmpty(tonewsdate))
				query.setParameter("tonewsdate", tonewsdate);
			if (search_topic_type != 0)
				query.setParameter("topicTypeId", search_topic_type);
			if (search_topic_status != 0)
				query.setParameter("statusTopicId", search_topic_status);

			topics = query.list();

		} catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		session.getTransaction().commit();
		return topics;
	}

	@SuppressWarnings("unchecked")
	public List<Object> listalltopicSearch(String search_field,
			String search_author, int city, int district, String fromnewsdate,
			String tonewsdate, int search_topic_type, String search_order_d,
			int search_topic_status) {

		// Hiện lên ở trang chủ
		// Nội dung các tin đăng
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.flush();
		session.clear();
		session.beginTransaction();
		List<Object> topics = null;
		try {
			StringBuffer hsql = new StringBuffer();
			hsql.append("select t.title, t.address, t.viewNum,DATE_FORMAT( t.createDate, '%Y-%m-%d'),t.topicId, t.topicTypeId,to.topicTypeName , t.districtId, u.displayName, d.districtName, t.usersId, d.provinceId,p.provinceName,s.statusTopicName,t.statusTopicId from Topics t,Topictypes to, Districts d, Users u,Provinces p,Statustopics s ");
			hsql.append("where p.provinceId = d.provinceId and t.districtId = d.districtId and t.usersId = u.usersId and to.topicTypeId = t.topicTypeId and t.statusTopicId = s.statusTopicId ");
			if (!StringUtil.isEmpty(search_field))
				hsql.append("AND LOWER(t.title) LIKE :title ");
			if (!StringUtil.isEmpty(search_author))
				hsql.append("AND LOWER(u.displayName) LIKE :displayName ");
			if (city != 0)
				hsql.append("AND LOWER(d.provinceId) = :provinceId ");
			if (district != 0)
				hsql.append("AND LOWER(t.districtId) = :districtId ");
			if (!StringUtil.isEmpty(fromnewsdate))
				hsql.append("AND DATE_FORMAT( t.createDate, '%Y-%m-%d') >= :fromnewsdate ");
			if (!StringUtil.isEmpty(tonewsdate))
				hsql.append("AND DATE_FORMAT( t.createDate, '%Y-%m-%d') <= :tonewsdate ");
			if (search_topic_type != 0)
				hsql.append("AND LOWER(t.topicTypeId) = :topicTypeId ");
			if (search_topic_status != 0)
				hsql.append("AND LOWER(t.statusTopicId) = :statusTopicId ");

			String order = "";
			order = "order by t.renewDate desc";
			if ("asc".equals(search_order_d))
				order = "order by t.renewDate asc";
			hsql.append(order);

			Query query = session.createQuery(hsql.toString());
			if (!StringUtil.isEmpty(search_field))
				query.setParameter("title", "%" + search_field + "%");
			if (!StringUtil.isEmpty(search_author))
				query.setParameter("displayName", "%" + search_author + "%");
			if (city != 0)
				query.setParameter("provinceId", city);
			if (district != 0)
				query.setParameter("districtId", district);
			if (!StringUtil.isEmpty(fromnewsdate))
				query.setParameter("fromnewsdate", fromnewsdate);
			if (!StringUtil.isEmpty(tonewsdate))
				query.setParameter("tonewsdate", tonewsdate);
			if (search_topic_type != 0)
				query.setParameter("topicTypeId", search_topic_type);
			if (search_topic_status != 0)
				query.setParameter("statusTopicId", search_topic_status);

			topics = query.list();

		} catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		session.getTransaction().commit();
		return topics;
	}

	@SuppressWarnings("unchecked")
	public List<Object> listalltopic() {

		// Hiện lên ở trang chủ
		// Nội dung các tin đăng
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<Object> topics = null;
		try {

			topics = (List<Object>) session
					.createQuery(
							"select t.title, t.address, t.viewNum,DATE_FORMAT( t.createDate, '%Y-%m-%d'),t.topicId, t.topicTypeId,to.topicTypeName , t.districtId, u.displayName, d.districtName, t.usersId, d.provinceId,p.provinceName,s.statusTopicName,t.statusTopicId "
									+ "from Topics t,Topictypes to, Districts d, Users u,Provinces p,Statustopics s "
									+ "where p.provinceId = d.provinceId and t.districtId = d.districtId and t.usersId = u.usersId and to.topicTypeId = t.topicTypeId  and t.statusTopicId = s.statusTopicId order by t.renewDate desc")
					.list();

		} catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		session.getTransaction().commit();
		return topics;
	}

	@SuppressWarnings("unchecked")
	public List<Object> listRent(int sortby) {

		// Hiện lên ở trang chủ
		// Nội dung các tin đăng
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<Object> topics = null;
		try {

			if (sortby == 0) {
				topics = (List<Object>) session
						.createQuery(
								"select t.title, t.address, t.price, t.area, t.viewNum, t.createDate, t.latitude, t.longitude, t.image, t.topicId, t.description, t.topicTypeId , t.districtId, u.displayName, d.districtName, t.usersId, d.provinceId from Topics t, Districts d, Users u where t.statusTopicId = 1 and t.districtId = d.districtId and t.usersId = u.usersId order by t.renewDate desc")
						.list();
			}
			if (sortby == 1) {
				topics = (List<Object>) session
						.createQuery(
								"select t.title, t.address, t.price, t.area, t.viewNum, t.createDate, t.latitude, t.longitude, t.image, t.topicId, t.description, t.topicTypeId , t.districtId, u.displayName, d.districtName, t.usersId, d.provinceId from Topics t, Districts d, Users u where t.statusTopicId = 1 and t.districtId = d.districtId and t.usersId = u.usersId order by t.price asc")
						.list();
			}
			if (sortby == 2) {
				topics = (List<Object>) session
						.createQuery(
								"select t.title, t.address, t.price, t.area, t.viewNum, t.createDate, t.latitude, t.longitude, t.image, t.topicId, t.description, t.topicTypeId , t.districtId, u.displayName, d.districtName, t.usersId, d.provinceId from Topics t, Districts d, Users u where t.statusTopicId = 1 and t.districtId = d.districtId and t.usersId = u.usersId order by t.price desc")
						.list();
			}
			if (sortby == 3) {
				topics = (List<Object>) session
						.createQuery(
								"select t.title, t.address, t.price, t.area, t.viewNum, t.createDate, t.latitude, t.longitude, t.image, t.topicId, t.description, t.topicTypeId , t.districtId, u.displayName, d.districtName, t.usersId, d.provinceId from Topics t, Districts d, Users u where t.statusTopicId = 1 and t.districtId = d.districtId and t.usersId = u.usersId order by t.viewNum desc")
						.list();
			}
			if (sortby == 4) {
				topics = (List<Object>) session
						.createQuery(
								"select t.title, t.address, t.price, t.area, t.viewNum, t.createDate, t.latitude, t.longitude, t.image, t.topicId, t.description, t.topicTypeId , t.districtId, u.displayName, d.districtName, t.usersId, d.provinceId from Topics t, Districts d, Users u where t.statusTopicId = 1 and t.districtId = d.districtId and t.usersId = u.usersId order by t.area desc")
						.list();
			}
			if (sortby == 5) {
				topics = (List<Object>) session
						.createQuery(
								"select t.title, t.address, t.price, t.area, t.viewNum, t.createDate, t.latitude, t.longitude, t.image, t.topicId, t.description, t.topicTypeId , t.districtId, u.displayName, d.districtName, t.usersId, d.provinceId from Topics t, Districts d, Users u where t.statusTopicId = 1 and t.districtId = d.districtId and t.usersId = u.usersId order by t.area asc")
						.list();
			}
		} catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		session.getTransaction().commit();
		return topics;
	}

	@SuppressWarnings("unchecked")
	public List<Object> listShare() {

		// Hiện lên ở trang chủ
		// Nội dung các tin đăng
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<Object> topics = null;
		try {

			topics = (List<Object>) session
					.createQuery(
							"select t.title, t.address, t.price, t.area, t.viewNum, t.createDate, t.latitude, t.longitude, t.image, t.topicId, t.description, t.topicTypeId , t.districtId, u.displayName, d.districtName, t.usersId , d.provinceId from Topics t, Districts d, Users u where t.statusTopicId = 1 and t.districtId = d.districtId and t.usersId = u.usersId and t.topicTypeId = 2 order by t.renewDate desc")
					.list();

		} catch (HibernateException e) {
			e.printStackTrace();
		}
		session.getTransaction().commit();
		return topics;
	}

	@SuppressWarnings("unchecked")
	public List<Float> getMinPrice() {

		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<Float> topics = null;
		try {

			topics = (List<Float>) session.createQuery(
					"select min(t.price) from Topics t").list();

		} catch (HibernateException e) {
			e.printStackTrace();
		}
		session.getTransaction().commit();
		return topics;
	}

	// Hiện các tin đã xem
	@SuppressWarnings("unchecked")
	public List<Object> listRecentView(String ipaddress) {

		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<Object> recentView = null;
		try {

			recentView = (List<Object>) session
					.createQuery(
							"select DISTINCT t.title, t.address, t.viewNum, t.image, t.topicId, t.districtId  from Topics t , Activityhistorys a where t.topicId = a.topicId and a.ipAddress like '%"
									+ ipaddress
									+ "%' and date(a.createDate) = CURDATE() order by a.createDate desc")
					.setMaxResults(5).list();

		} catch (HibernateException e) {
			e.printStackTrace();
		}
		session.getTransaction().commit();
		return recentView;
	}

	// Hiện các tin đã xem
	@SuppressWarnings("unchecked")
	public List<Object> listMostView() {

		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<Object> mostView = null;
		try {

			mostView = (List<Object>) session
					.createQuery(
							"select t.title, t.address, t.viewNum, t.image, t.topicId, t.districtId  from Topics t order by t.viewNum desc")
					.setMaxResults(5).list();

		} catch (HibernateException e) {
			e.printStackTrace();
		}
		session.getTransaction().commit();
		return mostView;
	}

	// Hiện các district
	@SuppressWarnings("unchecked")
	public List<Districts> listDistricts() {

		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<Districts> districts = null;
		try {

			districts = (List<Districts>) session
					.createQuery(
							"select d.provinceId, d.districtId, d.districtName from Districts d")
					.list();

		} catch (HibernateException e) {
			e.printStackTrace();
		}
		session.getTransaction().commit();
		return districts;
	}

	// Hiện các thành phố
	@SuppressWarnings("unchecked")
	public List<Provinces> listProvinces() {

		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<Provinces> provinces = null;
		try {

			provinces = (List<Provinces>) session.createQuery(
					"select p.provinceId, p.provinceName from Provinces p")
					.list();

		} catch (HibernateException e) {
			e.printStackTrace();
		}
		session.getTransaction().commit();
		return provinces;
	}

	// Trang detail

	@SuppressWarnings("unchecked")
	public List<Object> checkOwnerTopic(String email, int topicId) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<Object> checkOwnerTopic = null;
		try {
			checkOwnerTopic = (List<Object>) session
					.createQuery(
							"select u.email,t.usersId from Topics t, Users u where t.usersId = u.usersId and u.email ='"
									+ email + "' and t.topicId = " + topicId)
					.list();

		} catch (HibernateException e) {
			e.printStackTrace();
		}
		session.getTransaction().commit();
		return checkOwnerTopic;
	}

	// get email of users
	@SuppressWarnings("unchecked")
	public List<Object> getEmail(int topicId) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<Object> tp = null;
		try {
			tp = (List<Object>) session
					.createQuery(
							"select t.title, u.email from Topics t, Users u where t.usersId = u.usersId and t.topicId = "
									+ topicId).list();

		} catch (HibernateException e) {
			e.printStackTrace();
		}
		session.getTransaction().commit();
		return tp;
	}

	// check email admin
	@SuppressWarnings("unchecked")
	public List<Object> checkAdmin(String email) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<Object> checkAdmin = null;
		try {
			checkAdmin = (List<Object>) session.createQuery(
					"select u.email from Users u where u.roleId = 1 and u.email = '"
							+ email + "'").list();

		} catch (HibernateException e) {
			e.printStackTrace();
		}
		session.getTransaction().commit();
		return checkAdmin;
	}

	@SuppressWarnings("unchecked")
	public List<Object> detail(int topicId) {

		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<Object> topicdetail = null;
		try {

			topicdetail = (List<Object>) session
					.createQuery(
							"select t.title, t.address, t.area, t.price, t.contact, t.phone, t.viewNum, t.description, u.displayName, t.createDate , t.editDate , t.renewDate, t.latitude, t.longitude, t.image, t.topicId, t.topicTypeId, t.water, t.power, t.toilet, t.internet, t.houseType, t.motor, t.motorPrice, t.hour, t.owner, t.cook, t.usersId from Topics t, Users u where t.usersId = u.usersId and t.topicId = "
									+ topicId).list();

		} catch (HibernateException e) {
			e.printStackTrace();
		}
		session.getTransaction().commit();
		return topicdetail;
	}

	@SuppressWarnings("unchecked")
	public List<Topics> checkStatusTopic(int topicId) {

		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<Topics> topic = null;
		try {

			topic = (List<Topics>) session.createQuery(
					"from Topics t where t.topicId = " + topicId).list();

		} catch (HibernateException e) {
			e.printStackTrace();
		}
		session.getTransaction().commit();
		return topic;
	}

	@SuppressWarnings("unchecked")
	public List<Favorites> checkExitFav(int topicId, int usersId) {

		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<Favorites> fav = null;
		try {

			fav = (List<Favorites>) session.createQuery(
					"from Favorites f where f.topicId = " + topicId
							+ " and f.usersId = " + usersId).list();

		} catch (HibernateException e) {
			e.printStackTrace();
		}
		session.getTransaction().commit();
		return fav;
	}

	// get topic info
	@SuppressWarnings("unchecked")
	public List<Topics> topicinfo(int topicId) {

		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<Topics> topicdetail = null;
		try {

			topicdetail = (List<Topics>) session.createQuery(
					"from Topics t where t.topicId = " + topicId).list();

		} catch (HibernateException e) {
			e.printStackTrace();
		}
		session.getTransaction().commit();
		return topicdetail;
	}

	@SuppressWarnings("unchecked")
	public List<Topics> topicSameDistrict(int districtId, int topicId) {

		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<Topics> topicSame = null;
		try {

			topicSame = (List<Topics>) session
					.createQuery(
							"select t.title, t.address, t.viewNum, t.image, t.topicId, t.districtId from Topics t where t.districtId = "
									+ districtId
									+ " and t.topicId != "
									+ topicId + " order by t.editDate desc")
					.setMaxResults(9).list();

		} catch (HibernateException e) {
			e.printStackTrace();
		}
		session.getTransaction().commit();
		return topicSame;
	}

	@SuppressWarnings("unchecked")
	public List<Topicimages> image(int topicId) {

		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<Topicimages> images = null;
		try {

			images = (List<Topicimages>) session.createQuery(
					"select t.topicImageId, t.topicId, t.path from Topicimages t where t.topicId ="
							+ topicId).list();

		} catch (HibernateException e) {
			e.printStackTrace();
		}
		session.getTransaction().commit();
		return images;
	}

	@SuppressWarnings("unchecked")
	public List<Object> listUniversity(int districtId) {

		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<Object> universities = null;
		try {

			universities = (List<Object>) session
					.createQuery(
							"select l.locationName, l.description, l.latitude, l.longitude from Locations l where l.locationTypeId = 1 and l.districtId = "
									+ districtId).list();

		} catch (HibernateException e) {
			e.printStackTrace();
		}
		session.getTransaction().commit();
		return universities;
	}

	@SuppressWarnings("unchecked")
	public List<Object> listMarkets(int districtId) {

		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<Object> markets = null;
		try {

			markets = (List<Object>) session
					.createQuery(
							"select l.locationName, l.description, l.latitude, l.longitude from Locations l where l.locationTypeId = 3 and l.districtId = "
									+ districtId).list();

		} catch (HibernateException e) {
			e.printStackTrace();
		}
		session.getTransaction().commit();
		return markets;
	}

	@SuppressWarnings("unchecked")
	public List<Object> listParks(int districtId) {

		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<Object> parks = null;
		try {

			parks = (List<Object>) session
					.createQuery(
							"select l.locationName, l.description, l.latitude, l.longitude from Locations l where l.locationTypeId = 2 and l.districtId = "
									+ districtId).list();

		} catch (HibernateException e) {
			e.printStackTrace();
		}
		session.getTransaction().commit();
		return parks;
	}

	@SuppressWarnings("unchecked")
	public List<Object> listBus(int districtId) {

		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<Object> bus = null;
		try {

			bus = (List<Object>) session
					.createQuery(
							"select l.locationName, l.description, l.latitude, l.longitude from Locations l where l.locationTypeId = 4 and l.districtId = "
									+ districtId).list();

		} catch (HibernateException e) {
			e.printStackTrace();
		}
		session.getTransaction().commit();
		return bus;
	}

	@SuppressWarnings("unchecked")
	public List<Object> getRate(int topicId) {

		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<Object> rate = null;
		try {

			rate = (List<Object>) session
					.createQuery(
							"select AVG(r.pointRate) , COUNT(r.pointRate) from Ratetopics r where r.topicId ="
									+ topicId).list();

		} catch (HibernateException e) {
			e.printStackTrace();
		}
		session.getTransaction().commit();
		return rate;
	}

	// insert to Activityhistorys;
	public Activityhistorys add(Activityhistorys activityhistory) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		session.save(activityhistory);
		session.getTransaction().commit();
		return activityhistory;
	}

	// check exits in Activityhistorys;
	public boolean checkExistActivityhistorys(String ipaddress, int topicId) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		boolean exits = true;
		String hql = "from Activityhistorys where date(createDate) = CURDATE() and ipAddress like '%"
				+ ipaddress
				+ "%' and topicId = "
				+ topicId
				+ " and activityTypeId = 1";
		Query query = session.createQuery(hql);
		int rowCount = query.list().size();
		if (rowCount == 0) {
			exits = false;
		} else {
			exits = true;
		}
		session.getTransaction().commit();
		return exits;
	}

	// insert to refreshtopic;
	public RefreshTopic addrefreshtopic(RefreshTopic refreshtopic) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		session.save(refreshtopic);
		session.getTransaction().commit();
		return refreshtopic;
	}

	// check refresh
	@SuppressWarnings("unchecked")
	public List<RefreshTopic> getrefreshTopic(int topicId) {

		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<RefreshTopic> refreshtopic = null;
		try {

			refreshtopic = (List<RefreshTopic>) session.createQuery(
					"from RefreshTopic r where r.topicId =" + topicId).list();

		} catch (HibernateException e) {
			e.printStackTrace();
		}
		return refreshtopic;
	}

	// fresh count+1 topic
	public boolean updaterefreshtopic(int topicId, int count) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		boolean refresh = false;
		try {
			String hql = "update RefreshTopic set countTopic = " + count
					+ " where topicId =" + topicId;
			Query query = session.createQuery(hql);
			int rowCount = query.executeUpdate();
			if (rowCount < 1) {
			} else {
				refresh = true;
			}

		} catch (HibernateException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		session.getTransaction().commit();
		return refresh;
	}

	// update renewdate topic
	public boolean updateTopicRenew(int topicId, String curdate) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		boolean refresh = false;
		try {
			String hql = "update Topics set renewDate = '" + curdate
					+ "',statusTopicId = 1 where topicId =" + topicId;
			Query query = session.createQuery(hql);
			int rowCount = query.executeUpdate();
			if (rowCount < 1) {
			} else {
				refresh = true;
			}

		} catch (HibernateException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		session.getTransaction().commit();
		return refresh;
	}

	// delete multi topic
	public boolean deletedMultiTopic(String[] topicIds) {
		for (String topicId : topicIds) {
			deletedTopicImg(Integer.parseInt(topicId));
			deletedTopicFav(Integer.parseInt(topicId));
			deletedTopicSpam(Integer.parseInt(topicId));
			deletedTopicRate(Integer.parseInt(topicId));
			deletedActivity(Integer.parseInt(topicId));
			deletedRefresh(Integer.parseInt(topicId));
			deletedTopic(Integer.parseInt(topicId));
		}

		return true;
	}

	// delete topic
	public boolean deletedTopic(int topicId) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		boolean delete = false;
		try {
			String hql = "delete from Topics where topicId =" + topicId;
			Query query = session.createQuery(hql);
			int rowCount = query.executeUpdate();
			if (rowCount < 1) {
			} else {
				delete = true;
			}

		} catch (HibernateException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		session.getTransaction().commit();
		return delete;
	}

	// delete topic img
	public boolean deletedImgTopic(int topicImageId) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		boolean delete = false;
		try {
			String hql = "delete from Topicimages where topicImageId ="
					+ topicImageId;
			Query query = session.createQuery(hql);
			int rowCount = query.executeUpdate();
			if (rowCount < 1) {
			} else {
				delete = true;
			}

		} catch (HibernateException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		session.getTransaction().commit();
		return delete;
	}

	// select path topic img
	@SuppressWarnings("unchecked")
	public List<Topicimages> topicimg(int topicImageId) {

		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<Topicimages> topicdetail = null;
		try {

			topicdetail = (List<Topicimages>) session
					.createQuery(
							"from Topicimages t where t.topicImageId = "
									+ topicImageId).list();

		} catch (HibernateException e) {
			e.printStackTrace();
		}
		return topicdetail;
	}

	@SuppressWarnings("unchecked")
	public List<Topictypes> topictype() {

		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<Topictypes> topictype = null;
		try {

			topictype = (List<Topictypes>) session.createQuery(
					"from Topictypes t").list();

		} catch (HibernateException e) {
			e.printStackTrace();
		}
		return topictype;
	}

	@SuppressWarnings("unchecked")
	public List<Statustopics> topicstatus() {

		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<Statustopics> topicstatus = null;
		try {

			topicstatus = (List<Statustopics>) session.createQuery(
					"from Statustopics s").list();

		} catch (HibernateException e) {
			e.printStackTrace();
		}
		return topicstatus;
	}

	// delete topic_spam
	public boolean deletedTopicSpam(int topicId) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		boolean delete = false;
		try {
			String hql = "delete from Reportspams where topicId =" + topicId;
			Query query = session.createQuery(hql);
			int rowCount = query.executeUpdate();
			if (rowCount < 1) {
			} else {
				delete = true;
			}

		} catch (HibernateException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		session.getTransaction().commit();
		return delete;
	}

	// delete topic_rate
	public boolean deletedTopicRate(int topicId) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		boolean delete = false;
		try {
			String hql = "delete from Ratetopics where topicId =" + topicId;
			Query query = session.createQuery(hql);
			int rowCount = query.executeUpdate();
			if (rowCount < 1) {
			} else {
				delete = true;
			}

		} catch (HibernateException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		session.getTransaction().commit();
		return delete;
	}

	// delete topic_view
	public boolean deletedTopicViewN(int topicId) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		boolean delete = false;
		try {
			String hql = "delete from Topicviewnum where topicId =" + topicId;
			Query query = session.createQuery(hql);
			int rowCount = query.executeUpdate();
			if (rowCount < 1) {
			} else {
				delete = true;
			}

		} catch (HibernateException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		session.getTransaction().commit();
		return delete;
	}

	// delete topic_fav
	public boolean deletedTopicFav(int topicId) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		boolean delete = false;
		try {
			String hql = "delete from Favorites where topicId =" + topicId;
			Query query = session.createQuery(hql);
			int rowCount = query.executeUpdate();
			if (rowCount < 1) {
			} else {
				delete = true;
			}

		} catch (HibernateException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		session.getTransaction().commit();
		return delete;
	}

	// delete all topic_fav
	public boolean deleteAllTopicFav(int usersId) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		boolean delete = false;
		try {
			String hql = "delete from Favorites where usersId =" + usersId;
			Query query = session.createQuery(hql);
			int rowCount = query.executeUpdate();
			if (rowCount < 1) {
			} else {
				delete = true;
			}

		} catch (HibernateException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		session.getTransaction().commit();
		return delete;
	}

	// delete topic_img
	public boolean deletedTopicImg(int topicId) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		boolean delete = false;
		try {
			String hql = "delete from Topicimages where topicId =" + topicId;
			Query query = session.createQuery(hql);
			int rowCount = query.executeUpdate();
			if (rowCount < 1) {
			} else {
				delete = true;
			}

		} catch (HibernateException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		session.getTransaction().commit();
		return delete;
	}

	// delete topic_activity
	public boolean deletedActivity(int topicId) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		boolean delete = false;
		try {
			String hql = "delete from Activityhistorys where topicId ="
					+ topicId;
			Query query = session.createQuery(hql);
			int rowCount = query.executeUpdate();
			if (rowCount < 1) {
			} else {
				delete = true;
			}

		} catch (HibernateException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		session.getTransaction().commit();
		return delete;
	}

	// delete refresh
	public boolean deletedRefresh(int topicId) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		boolean delete = false;
		try {
			String hql = "delete from RefreshTopic where topicId =" + topicId;
			Query query = session.createQuery(hql);
			int rowCount = query.executeUpdate();
			if (rowCount < 1) {
			} else {
				delete = true;
			}

		} catch (HibernateException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		session.getTransaction().commit();
		return delete;
	}

	// hide topic
	public boolean hideTopic(int topicId) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		boolean hide = false;
		try {
			String hql = "update Topics set statusTopicId = 2 where topicId ="
					+ topicId;
			Query query = session.createQuery(hql);
			int rowCount = query.executeUpdate();
			if (rowCount < 1) {
			} else {
				hide = true;
			}

		} catch (HibernateException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		session.getTransaction().commit();
		return hide;
	}

	// [delete] =viewNu
	public boolean updateViewNum(int topicId) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		boolean update = false;
		try {
			String hql = "update Topics set viewNum = viewNum + 1  where topicId ="
					+ topicId;
			Query query = session.createQuery(hql);
			int rowCount = query.executeUpdate();
			if (rowCount < 1) {
			} else {
				update = true;
			}

		} catch (HibernateException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		session.getTransaction().commit();
		return update;
	}

	// count topic
	@SuppressWarnings("unchecked")
	public List<Object> totalTopic() {

		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<Object> topic = null;
		try {

			topic = (List<Object>) session.createQuery("from Topics t").list();

		} catch (HibernateException e) {
			e.printStackTrace();
		}
		return topic;
	}

	@SuppressWarnings("unchecked")
	public List<Object> totalUser() {

		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<Object> topic = null;
		try {

			topic = (List<Object>) session.createQuery("from Users u").list();

		} catch (HibernateException e) {
			e.printStackTrace();
		}
		return topic;
	}

	@SuppressWarnings("unchecked")
	public List<Object> totalUserBaner() {

		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<Object> topic = null;
		try {

			topic = (List<Object>) session.createQuery(
					"from Users u where IsBanner=1").list();

		} catch (HibernateException e) {
			e.printStackTrace();
		}
		return topic;
	}
}
